<template>
  <div class="center">
    <!-- 头部 -->
    <div class="table_header flex flex_bet flex-wrap">
      <div class="table_button">
        <el-button type="primary" icon="el-icon-refresh" size="mini" @click="Refresh"></el-button>
        <el-button type="primary" icon="el-icon-plus" size="mini" @click="formVisible = true" v-if="isAdmin">添加</el-button>
      </div>
      <div class="table_searchs flex felx_align">
        <div class="table_search flex">
          <el-input placeholder="搜索" v-model="searchTable" class="input-with-select" size="small">
            <el-select v-model="select" slot="prepend" placeholder="请选择" class="table_select" style="width:105px">
              <el-option label="供应商名" value="1"></el-option>
            </el-select>
            <el-button slot="append" icon="el-icon-search" @click="idnameSearch"></el-button>
          </el-input>
        </div>
      </div>
    </div>

    <el-table stripe style="width: 100%"  border fit highlight-current-row ref="tableDataRef" :data="tableData" :key="key" >
      <el-table-column  min-width="50" align="center" prop="id" label="ID"></el-table-column>
      <el-table-column  min-width="50" align="center" prop="name" label="供应商名称">
      </el-table-column>
      <el-table-column  min-width="50" align="center" prop="update_time" label="更新时间">
        <template slot-scope="scope">{{scope.row.update_time| activateTime}}</template>
      </el-table-column>
      <el-table-column  min-width="50" align="center" prop="admin_name" label="操作账号"></el-table-column>
      <el-table-column v-if="isAdmin" label="操作" min-width="150" align="center">
        <template slot-scope="scope">
          <el-row>
            <el-button size="mini" type="primary" @click="openEdit(scope.row)">编辑</el-button>
            <el-button size="mini" @click="delSupplier(scope.row.id)">删除</el-button>
          </el-row>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <div class="paging">
      <el-pagination :current-page.sync="listQuery.startSize" background @size-change="handleSizeChange"
                     @current-change="handleCurrentChange" :page-sizes="[10, 50, 100, 200]"
                     layout="total, sizes, prev, pager, next, jumper" :total="total" :page-size="listQuery.pageSize">
      </el-pagination>
    </div>
    <!-- 添加弹窗 -->
    <el-dialog :title="editSubjects == true ? '编辑供应商' :'添加供应商'" :visible.sync="formVisible" :close-on-click-modal="false" :close-on-press-escape="false" width="40%" @close="addFormclose">
      <el-form :model="addForm"  ref="ruleForm" :rules="rules" class="diolog_adapt" style="width: 600px" >
        <el-form-item label="供应商名称:" label-width="120px" prop="name">
          <el-input v-model="addForm.name" placeholder="请输入供应商名称"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="formVisible = false" size="mini" round>取 消</el-button>
        <el-button v-if="editSubjects" type="primary" size="mini" round @click="editSupplier('ruleForm')">确 定
        </el-button>
        <el-button v-else type="primary" @click="addSupplier('ruleForm')" size="mini" round>确定</el-button>
      </div>
    </el-dialog>
    <delete-Dialog title="" :visible.sync="deleteDialogs.show" width="25%" top="40vh" @deleteEvents="deleteEvents" :deleteText="deleteDialogs.deleteText" />
  </div>

</template>

<script scoped>
  import dynamicTable from "@/components/DynamicTable";
  import deleteDialog from "@/components/deleteDialog";
  import tableMini from '@/utils/tableMini.js'
  import {
    addSupplierControl,
   querySupplierControl, editSupplierControl,delSupplierControl
  } from "@/api/launcherMade";

  export default {
    mixins: [tableMini],
    components: {dynamicTable, deleteDialog},
    data() {
      return {
        key: 1,
        colData: [], //动态控制table
        checkboxVal: ["ID", "图标", "课程名", "课程文件性质", "课程文件大小", "套餐归属", "说明", "适配学龄段"],
        searchTable: "",
        select: "", //搜索条件
        tableData: [],
        listQuery: {
          startSize: 1,
          pageSize: 10,
          userId: localStorage.getItem("ID"),
        },
        addForm: {
          name: "",
        },
        formVisible: false,
        rules: {
          name: [{required: true, message: '请输入供应商名称', trigger: 'blur'}, { min: 0, max: 12, message: '长度需在12个字符内', trigger: 'blur' }],
        },
        editSubjects: false,
        deleteDialogs: { show: false, deleteText: '', deleteId: 0 },
        isAdmin: true,
      }
    },
    created() {
      this.getSupplierTableData()
      if (localStorage.getItem("roleName") != '超级管理员' || localStorage.getItem("roleName") == '管理员') {
        this.isAdmin = false
      }
    },
    methods: {
      Refresh() {
        this.listQuery = { startSize: 1, pageSize: 10, userId: localStorage.getItem("ID"), name: undefined}
        this.getSupplierTableData()
      },

      //搜索,1:课程名称
      idnameSearch() {
        if (!this.select) return this.$message.warning("请先选择查询条件");
        this.listQuery.startSize = 1
        this.listQuery.pageSize = 10
        switch (this.select) {
          case '1':
            this.listQuery.name = this.searchTable
            this.getSupplierTableData();
            break;
        }
      },
      openEdit(row) {
        this.formVisible = true
        this.editSubjects=true
        this.addForm=row
      },
      addSupplier(formName) {
        this.$refs[formName].validate(valid => {
          if (valid) {
            let addForm = JSON.parse(JSON.stringify(this.addForm))
            addForm.last_ip = sessionStorage.getItem("Ip")
            addForm.userId = localStorage.getItem("ID")
            addSupplierControl(addForm).then(res => {
              if(res.code==200){
                this.$message.success(res.msg)
              }
              this.formVisible = false
              this.getSupplierTableData()
            }).catch(err => {
            })
          } else {
            return false;
          }
        });
      },
      getSupplierTableData() {
        querySupplierControl(this.listQuery).then(res => {
          if(res.code==200){
            this.tableData = res.data
            this.total = res.total
          }else{
            this.$message.error(this.res.msg)
          }
        }).catch(err => {
          this.tableData = []
          this.total = 0
        })
      },
      editSupplier(formName) {
        this.$refs[formName].validate(valid => {
          if (valid) {
            let editForm = JSON.parse(JSON.stringify(this.addForm))
            editForm.last_ip = sessionStorage.getItem("Ip")
            editForm.userId = localStorage.getItem("ID")
            editSupplierControl(editForm).then(res => {
              this.$message.success(res.msg)
              this.formVisible = false
              this.getSupplierTableData()
            }).catch(err => {
            })
          } else {
            return false;
          }
        });
      },
      delSupplier(id) {
        this.deleteDialogs.show = true
        this.deleteDialogs.deleteId = id
      },
      //删除按钮事件
      deleteEvents() {
        let params = {
          userId: localStorage.getItem("ID"),
          last_ip: sessionStorage.getItem("Ip"),
          id: this.deleteDialogs.deleteId,
        }
        delSupplierControl(params).then(res => {
          if(res.code==200){
            this.$message.success(res.msg)
          }else{
            this.$message.error(res.msg)
          }
          this.getSupplierTableData()
        }).catch(err => {
          console.log("请求失败", err)
        })
      },
      //分页
      handleSizeChange(val) {
        this.listQuery.pageSize = val
        this.getTabularData()
      },
      handleCurrentChange(val) {
        this.listQuery.startSize = val
        this.getTabularData()
      },
      //编辑添加表单关闭事件
      addFormclose() {
        this.editSubjects=false
        this.$nextTick(() => {
          this.addForm = this.$options.data().addForm
        })
      },
    }
  }
</script>

<style scoped>
  .avatar-uploader .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
  }
  .avatar-uploader .el-upload:hover {
    border-color: #409EFF;
  }
  .avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 100px;
    height: 100px;
    line-height: 100px;
    text-align: center;
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
  }
  .avatar {
    width: 100px;
    height: 100px;
    display: block;
  }
</style>
